<template>
    <div class="main-content">
        <echartsLayout :formState='formState' :dataListState='dataListState' :eCharts1State='eCharts1State' :eCharts2State='eCharts2State' 
        :eCharts3State='eCharts3State' :chartsListState='chartsListState' :tableState='tableState'>
            <div slot="form">
                <el-form :model="query" ref="query" label-width="45px" label-position="right">
                    <el-row :gutter="41">
                        <el-col :span="7">
                            <el-form-item label="月份">
                                 <el-date-picker
                                    v-model="query.giveTimeStart"
                                    style="width:100%"
                                    :picker-options="query.pickerOptions1" 
                                    :clearable="false" 
                                    :editable="false" 
                                    type="month"
                                    placeholder="选择月份">
                                </el-date-picker>
                            </el-form-item>
                        </el-col>
                        <el-col :span="2">
                            <div class="top-line">
                                <el-button type="primary" class="btn-common" @click="onSubmit">查询</el-button>
                            </div>
                        </el-col>
                    </el-row> 
                </el-form>
            </div> 
            <div slot="table" >
                <div class="table-title">
                    <exportExcel @ee="exportFile"></exportExcel>
                </div> 
                <el-table :data="tableData" border v-loading="loading" style="width: 100%" @sort-change='sortChange'>
                    <el-table-column label="新增用户数" align="center" sortable='custom' prop="newUserNumber">
                        <template scope="scope">
                            <span>{{scope.row.newUserNumber| isNull}}</span>
                        </template>
                    </el-table-column>

                    <el-table-column label="次日留存率" align="center" sortable='custom' prop="day1">
                        <template scope="scope">
                            <span>{{scope.row.day1+'%'| isNull}}</span>
                        </template>
                    </el-table-column>

                    <el-table-column label="7日留存率" align="center" sortable='custom' prop="day7">
                        <template scope="scope">
                            <span>{{scope.row.day7+'%'| isNull}}</span>
                        </template>
                    </el-table-column>
    
                    <el-table-column label="14日留存率" align="center" sortable='custom' prop="day14">
                        <template scope="scope">
                            <span>{{scope.row.day14+'%'| isNull}}</span>
                        </template>
                    </el-table-column>

                    <el-table-column label="30日留存率" align="center" sortable='custom' prop="day30">
                        <template scope="scope">
                            <span>{{scope.row.day30+'%'| isNull}}</span>
                        </template>
                    </el-table-column>

                    <el-table-column label="新增用户日期" align="center">
                        <template scope="scope">
                            <span>{{scope.row.date| isNull}}</span>
                        </template>
                    </el-table-column>
                </el-table>
                <div class="pagination-container">
                    <el-pagination
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="pageQuery.total"
                        :current-page.sync="pageQuery.currentpage"
                        :page-sizes="config.pageSizes"
                        :page-size="config.pageSize"
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange">
                    </el-pagination>
                </div>
            </div>
        </echartsLayout>
    </div>
</template>

<script>
    import moment from 'moment';
    import echartsLayout from 'components/TableLayout/echartsLayout'; 
    import exportExcel from 'components/ExportExcel'; 
    import { mapState, mapActions } from 'vuex';
    import { findUserRetentionCount } from 'api/pingtaifenxi';
    export default{
        name: 'statisticsView',
        components: {echartsLayout, exportExcel},
        data() {
            return {
                loading: false,
                formState: false,
                dataListState: true,
                eCharts1State: true,
                eCharts2State: true,
                eCharts3State: true,
                chartsListState: true,
                tableState: false,
                schoolList: [],
                query: {
                    // startDate: moment(new Date()).add(-30, 'days').format('YYYY-MM-DD'),
                    // endDate: moment(new Date()).add(-1, 'days').format('YYYY-MM-DD'),
                    giveTimeStart: moment(new Date()).format('YYYY-MM'),
                    pickerOptions1: {
                        disabledDate(time) {
                            return (time.getTime()) > (Date.now());
                        }
                    }
                },
                tableData: [],
                pageQuery: {
                    currentpage: 1,
                    total: 0
                },
                pickerOptions1: null,
                pickerOptions2: null,
                userNumberStatus: '',
                nextDayRateStatus: '',
                dayRate7Status: '',
                dayRate14Status: '',
                dayRate30Status: ''
            }
        },
        computed: {
            ...mapState({
                config: state => state.config.table
            })
        },
        created() {
            this.queryData();
        },
        methods: {
            ...mapActions(['ChangePageSize']),
            handleSizeChange(val) {
                this.pageQuery.currentpage = 1;
                this.ChangePageSize(val);
                this.queryData();
            },
            handleCurrentChange(val) {
                this.pageQuery.currentpage = val;
                this.queryData();
            },
            exportFile() {
                let date=moment(this.query.giveTimeStart).format('YYYY-MM'),
                    userNumberStatus=this.userNumberStatus,
                    nextDayRateStatus=this.nextDayRateStatus,
                    dayRate7Status=this.dayRate7Status,
                    dayRate14Status=this.dayRate14Status,
                    dayRate30Status=this.dayRate30Status;
                window.location.href = `${LOCAL_API.scy}/report/user/outExcelUserRetentionCount?date=${date}&userNumberStatus=${userNumberStatus}&nextDayRateStatus=${nextDayRateStatus}&dayRate7Status=${dayRate7Status}&dayRate14Status=${dayRate14Status}&dayRate30Status=${dayRate30Status}`
            },
            queryData() {
                this.loading = true;
                let page=this.pageQuery.currentpage,
                    pageSize=this.config.pageSize,
                    date=moment(this.query.giveTimeStart).format('YYYY-MM'),
                    userNumberStatus=this.userNumberStatus,
                    nextDayRateStatus=this.nextDayRateStatus,
                    dayRate7Status=this.dayRate7Status,
                    dayRate14Status=this.dayRate14Status,
                    dayRate30Status=this.dayRate30Status;
                findUserRetentionCount(page, pageSize, date, userNumberStatus, nextDayRateStatus, dayRate7Status, dayRate14Status, dayRate30Status).then(res => {
                    this.loading = false;
                    this.tableData = res.data.content.list;
                    this.pageQuery.total =res.data.content.pagination? res.data.content.pagination.total:0;
                }).catch(() => {
                    this.loading = false;
                    this.tableData = [];
                });
            },
            onSubmit() {
                this.pageQuery.currentpage = 1;
                this.queryData();
            },
            sortChange({ column, prop, order }) {
                this.userNumberStatus=prop=='newUserNumber'?order=='ascending'?'0':'1':'';
                this.nextDayRateStatus =prop=='day1'?order=='ascending'?'0':'1':'';
                this.dayRate7Status =prop=='day7'?order=='ascending'?'0':'1':'';
                this.dayRate14Status =prop=='day14'?order=='ascending'?'0':'1':'';
                this.dayRate30Status =prop=='day30'?order=='ascending'?'0':'1':'';
                this.pageQuery.currentpage = 1;
                this.queryData();
            }
        }
    }
</script>
<style lang="scss" scoped>
    .main-content {
        position: relative;
        $bgcolor: #fff;
    }
</style>                     